package com.wxzhou.wangzhuan.service;

import com.wxzhou.wangzhuan.cache.NoneCacheKeyGenerator;
import com.wxzhou.wangzhuan.constants.CacheNames;
import com.wxzhou.wangzhuan.dto.config.*;
import com.wxzhou.wangzhuan.repository.ConfigRepository;
import io.quarkus.cache.CacheResult;
import io.smallrye.mutiny.Uni;
import io.vertx.core.json.Json;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import org.jboss.logging.Logger;

/**
 * 系统配置
 */
@ApplicationScoped
public class ConfigService {

    private static final Logger LOG = Logger.getLogger(ConfigService.class);

    @Inject
    ConfigRepository configRepository;


    /**
     * 获取提现配置
     *
     * @return
     */
    @CacheResult(cacheName = CacheNames.CACHE_WITHDRAW_CONFIG, keyGenerator = NoneCacheKeyGenerator.class)
    public Uni<WithdrawConfigDto> getWithdrawConfig() {
        return configRepository.getConfigByKey(CacheNames.CACHE_WITHDRAW_CONFIG)
                .onItem().transformToUni(optionalConfig -> {
                    if (optionalConfig.isPresent()) {
                        AppConfig config = optionalConfig.get();
                        WithdrawConfigDto adConfig = Json.decodeValue(config.getConfigValue(), WithdrawConfigDto.class);
                        return Uni.createFrom().item(adConfig);
                    } else {
                        LOG.errorf("获取提现配置异常");
                        throw new RuntimeException("系统异常");
                    }
                });
    }

    /**
     * 获取系统配置
     *
     * @return
     */
    @CacheResult(cacheName = CacheNames.CACHE_SYSTEM_CONFIG, keyGenerator = NoneCacheKeyGenerator.class)
    public Uni<SystemConfigDto> getSystemConfig() {
        return configRepository.getConfigByKey(CacheNames.CACHE_SYSTEM_CONFIG)
                .onItem().transformToUni(optionalConfig -> {
                    if (optionalConfig.isPresent()) {
                        AppConfig config = optionalConfig.get();
                        SystemConfigDto adConfig = Json.decodeValue(config.getConfigValue(), SystemConfigDto.class);
                        return Uni.createFrom().item(adConfig);
                    } else {
                        LOG.errorf("获取系统配置异常");
                        throw new RuntimeException("系统异常");
                    }
                });
    }

    /**
     * 获取三方支付配置
     *
     * @return
     */
    @CacheResult(cacheName = CacheNames.CACHE_THIRD_PAY_CONFIG, keyGenerator = NoneCacheKeyGenerator.class)
    public Uni<ThirdPayConfigDto> getThirdPayConfig() {
        return configRepository.getConfigByKey(CacheNames.CACHE_THIRD_PAY_CONFIG)
                .onItem().transformToUni(optionalConfig -> {
                    if (optionalConfig.isPresent()) {
                        AppConfig config = optionalConfig.get();
                        ThirdPayConfigDto adConfig = Json.decodeValue(config.getConfigValue(), ThirdPayConfigDto.class);
                        return Uni.createFrom().item(adConfig);
                    } else {
                        LOG.errorf("获取支付配置异常");
                        throw new RuntimeException("系统异常");
                    }
                });
    }

    /**
     * 获取阅读配置
     *
     * @return
     */
    @CacheResult(cacheName = CacheNames.CACHE_READER_CONFIG, keyGenerator = NoneCacheKeyGenerator.class)
    public Uni<ReaderConfigDto> getReaderConfig() {
        return configRepository.getConfigByKey(CacheNames.CACHE_READER_CONFIG)
                .onItem().transformToUni(optionalConfig -> {
                    if (optionalConfig.isPresent()) {
                        AppConfig config = optionalConfig.get();
                        ReaderConfigDto adConfig = Json.decodeValue(config.getConfigValue(), ReaderConfigDto.class);
                        return Uni.createFrom().item(adConfig);
                    } else {
                        LOG.errorf("获取阅读配置异常");
                        throw new RuntimeException("系统异常");
                    }
                });
    }

}
